package com.nulldev.util.privacy;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

import com.nulldev.util.java.DataSet;
import com.nulldev.util.java.swing.extSwing.eActiveConsoledBar;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.math.MathUtil;
import com.nulldev.util.other.ArrayUtils;
import com.nulldev.util.uri.URIUtil;
import com.nulldev.util.uri.URIUtil2;

public class IdentityGenerator {

	private static final String[] FIRST_NAMES = ("Adam\n" + 
			"Adrian\n" + 
			"Alan\n" + 
			"Alexander\n" + 
			"Andrew\n" + 
			"Anthony\n" + 
			"Austin\n" + 
			"Benjamin\n" + 
			"Blake\n" + 
			"Boris\n" + 
			"Brandon\n" + 
			"Brian\n" + 
			"Cameron\n" + 
			"Carl\n" + 
			"Charles\n" + 
			"Christian\n" + 
			"Christopher\n" + 
			"Colin\n" + 
			"Connor\n" + 
			"Dan\n" + 
			"David\n" + 
			"Dominic\n" + 
			"Dylan\n" + 
			"Edward\n" + 
			"Eric\n" + 
			"Evan\n" + 
			"Frank\n" + 
			"Gavin\n" + 
			"Gordon\n" + 
			"Harry\n" + 
			"Ian\n" + 
			"Isaac\n" + 
			"Jack\n" + 
			"Jacob\n" + 
			"Jake\n" + 
			"James\n" + 
			"Jason\n" + 
			"Joe\n" + 
			"John\n" + 
			"Jonathan\n" + 
			"Joseph\n" + 
			"Joshua\n" + 
			"Julian\n" + 
			"Justin\n" + 
			"Keith\n" + 
			"Kevin\n" + 
			"Leonard\n" + 
			"Liam\n" + 
			"Lucas\n" + 
			"Luke\n" + 
			"Matt\n" + 
			"Max\n" + 
			"Michael\n" + 
			"Nathan\n" + 
			"Neil\n" + 
			"Nicholas\n" + 
			"Oliver\n" + 
			"Owen\n" + 
			"Paul\n" + 
			"Peter\n" + 
			"Phil\n" + 
			"Piers\n" + 
			"Richard\n" + 
			"Robert\n" + 
			"Ryan\n" + 
			"Sam\n" + 
			"Sean\n" + 
			"Sebastian\n" + 
			"Simon\n" + 
			"Stephen\n" + 
			"Steven\n" + 
			"Stewart\n" + 
			"Thomas\n" + 
			"Tim\n" + 
			"Trevor\n" + 
			"Victor\n" + 
			"Warren\n" + 
			"William\n" +
			"Abigail\n" + 
			"Alexandra\n" + 
			"Alison\n" + 
			"Amanda\n" + 
			"Amelia\n" + 
			"Amy\n" + 
			"Andrea\n" + 
			"Angela\n" + 
			"Anna\n" + 
			"Anne\n" + 
			"Audrey\n" + 
			"Ava\n" + 
			"Bella\n" + 
			"Bernadette\n" + 
			"Carol\n" + 
			"Caroline\n" + 
			"Carolyn\n" + 
			"Chloe\n" + 
			"Claire\n" + 
			"Deirdre\n" + 
			"Diana\n" + 
			"Diane\n" + 
			"Donna\n" + 
			"Dorothy\n" + 
			"Elizabeth\n" + 
			"Ella\n" + 
			"Emily\n" + 
			"Emma\n" + 
			"Faith\n" + 
			"Felicity\n" + 
			"Fiona\n" + 
			"Gabrielle\n" + 
			"Grace\n" + 
			"Hannah\n" + 
			"Heather\n" + 
			"Irene\n" + 
			"Jan\n" + 
			"Jane\n" + 
			"Jasmine\n" + 
			"Jennifer\n" + 
			"Jessica\n" + 
			"Joan\n" + 
			"Joanne\n" + 
			"Julia\n" + 
			"Karen\n" + 
			"Katherine\n" + 
			"Kimberly\n" + 
			"Kylie\n" + 
			"Lauren\n" + 
			"Leah\n" + 
			"Lillian\n" + 
			"Lily\n" + 
			"Lisa\n" + 
			"Madeleine\n" + 
			"Maria\n" + 
			"Mary\n" + 
			"Megan\n" + 
			"Melanie\n" + 
			"Michelle\n" + 
			"Molly\n" + 
			"Natalie\n" + 
			"Nicola\n" + 
			"Olivia\n" + 
			"Penelope\n" + 
			"Pippa\n" + 
			"Rachel\n" + 
			"Rebecca\n" + 
			"Rose\n" + 
			"Ruth\n" + 
			"Sally\n" + 
			"Samantha\n" + 
			"Sarah\n" + 
			"Sonia\n" + 
			"Sophie\n" + 
			"Stephanie\n" + 
			"Sue\n" + 
			"Theresa\n" + 
			"Tracey\n" + 
			"Una\n" + 
			"Vanessa\n" + 
			"Victoria\n" + 
			"Virginia\n" + 
			"Wanda\n" + 
			"Wendy\n" + 
			"Yvonne\n" + 
			"Zoe").split("\n");
	
	public static final String[] LAST_NAMES = (
			"Abraham\n" + 
			"Allan\n" + 
			"Alsop\n" + 
			"Anderson\n" + 
			"Arnold\n" + 
			"Avery\n" + 
			"Bailey\n" + 
			"Baker\n" + 
			"Ball\n" + 
			"Bell\n" + 
			"Berry\n" + 
			"Black\n" + 
			"Blake\n" + 
			"Bond\n" + 
			"Bower\n" + 
			"Brown\n" + 
			"Buckland\n" + 
			"Burgess\n" + 
			"Butler\n" + 
			"Cameron\n" + 
			"Campbell\n" + 
			"Carr\n" + 
			"Chapman\n" + 
			"Churchill\n" + 
			"Clark\n" + 
			"Clarkson\n" + 
			"Coleman\n" + 
			"Cornish\n" + 
			"Davidson\n" + 
			"Davies\n" + 
			"Dickens\n" + 
			"Dowd\n" + 
			"Duncan\n" + 
			"Dyer\n" + 
			"Edmunds\n" + 
			"Ellison\n" + 
			"Ferguson\n" + 
			"Fisher\n" + 
			"Forsyth\n" + 
			"Fraser\n" + 
			"Gibson\n" + 
			"Gill\n" + 
			"Glover\n" + 
			"Graham\n" + 
			"Grant\n" + 
			"Gray\n" + 
			"Greene\n" + 
			"Hamilton\n" + 
			"Hardacre\n" + 
			"Harris\n" + 
			"Hart\n" + 
			"Hemmings\n" + 
			"Henderson\n" + 
			"Hill\n" + 
			"Hodges\n" + 
			"Howard\n" + 
			"Hudson\n" + 
			"Hughes\n" + 
			"Hunter\n" + 
			"Ince\n" + 
			"Jackson\n" + 
			"James\n" + 
			"Johnston\n" + 
			"Jones\n" + 
			"Kelly\n" + 
			"Kerr\n" + 
			"King\n" + 
			"Knox\n" + 
			"Lambert\n" + 
			"Langdon\n" + 
			"Lawrence\n" + 
			"Lee\n" + 
			"Lewis\n" + 
			"Lyman\n" + 
			"MacDonald\n" + 
			"Mackay\n" + 
			"Mackenzie\n" + 
			"MacLeod\n" + 
			"Manning\n" + 
			"Marshall\n" + 
			"Martin\n" + 
			"Mathis\n" + 
			"May\n" + 
			"McDonald\n" + 
			"McLean\n" + 
			"McGrath\n" + 
			"Metcalfe\n" + 
			"Miller\n" + 
			"Mills\n" + 
			"Mitchell\n" + 
			"Morgan\n" + 
			"Morrison\n" + 
			"Murray\n" + 
			"Nash\n" + 
			"Newman\n" + 
			"Nolan\n" + 
			"North\n" + 
			"Ogden\n" + 
			"Oliver\n" + 
			"Paige\n" + 
			"Parr\n" + 
			"Parsons\n" + 
			"Paterson\n" + 
			"Payne\n" + 
			"Peake\n" + 
			"Peters\n" + 
			"Piper\n" + 
			"Poole\n" + 
			"Powell\n" + 
			"Pullman\n" + 
			"Quinn\n" + 
			"Rampling\n" + 
			"Randall\n" + 
			"Rees\n" + 
			"Reid\n" + 
			"Roberts\n" + 
			"Robertson\n" + 
			"Ross\n" + 
			"Russell\n" + 
			"Rutherford\n" + 
			"Sanderson\n" + 
			"Scott\n" + 
			"Sharp\n" + 
			"Short\n" + 
			"Simpson\n" + 
			"Skinner\n" + 
			"Slater\n" + 
			"Smith\n" + 
			"Springer\n" + 
			"Stewart\n" + 
			"Sutherland\n" + 
			"Taylor\n" + 
			"Terry\n" + 
			"Thomson\n" + 
			"Tucker\n" + 
			"Turner\n" + 
			"Underwood\n" + 
			"Vance\n" + 
			"Vaughan\n" + 
			"Walker\n" + 
			"Wallace\n" + 
			"Walsh\n" + 
			"Watson\n" + 
			"Welch\n" + 
			"White\n" + 
			"Wilkins\n" + 
			"Wilson\n" + 
			"Wright\n" + 
			"Young"
			).split("\n");
	
	private static final String[] SEARCH_TERMS = {
			"soylent", 
			"how to get along with your wife's boyfriend",
			"bulldogs", 
			"cute cat photos",
			"facebook %fake.name%",
			"snapchat %fake.username%"
			,"fortnite", "minecraft", "dog food", "cat food",
			"Windows Insider", "travel globally", "reddit", "reddit the_donald", "amd fanboys", "nvidia rules",
			"discord", "discord hypesquad", "free minecraft hack", "free bitcoin", "bitcoin guide", "buy soylent btc",
			"hacker", "google", "twitch ninja", "twitch fortnite", "twitch battle royale", "blacked.com", "reddit.com",
			"facebook.com", "twitter", "maga", "twitter donald trump", "gaming news", "i hate white people", "buzzfeed",
			"nintendo switch", "nintendo labo", "local jobs", "jidf", "adf", "eurovision", "summit1g", "shroud", "cia \u006e\u0069\u0067\u0067\u0065\u0072\u0073",
			"black panther merch", "marvel merch", "infinity war merch", "how to report hate crime", "anti semites report", "tumblr", "tumblr shut down 4chan"
	};
	
	public static String processVars(final String input) {
		final DataSet<String, String> name = generateName();
		return input.replaceAll("%fake.name%", name.getA() + " " + name.getB()).replaceAll("%fake.username%", username(name));
	}
	
	public static String username(final DataSet<String, String> set) {
		final Random rnd = new Random();
		
		String result;

	    result = Character.toString(set.getA().charAt(0)); // First char
	    if (set.getB().length() > 5)
	      result += set.getB().substring(0, 5);
	    else
	      result += set.getB();
	    result += Integer.toString(rnd.nextInt(99));
	    
	    return result;
	}
	
	public static DataSet<String, String> generateName() {
		return new DataSet<String, String>(ArrayUtils.random(FIRST_NAMES).replaceAll("\n", ""), ArrayUtils.random(LAST_NAMES).replaceAll("\n", ""));
	}
	
	public static String createGoogleSearch(final String term) {
		return "http://www.google.com/search?q=" + term.replaceAll(" ", "+");
	}
	
	public static String createDuckDuckGoSearch(final String term) {
		return "https://duckduckgo.com/?q=" + term.replaceAll(" ", "+");
	}
	
	public static String createBingSearch(final String term) {
		return "https://www.bing.com/search?q=" + term.replaceAll(" ", "+");
	}
	
	public static String createYoutubeSearch(final String term) {
		return "https://www.youtube.com/results?search_query=" + term.replaceAll(" ", "+");
	}
	
	private static String gt_int(final int ug) {
		if (ug == 0) {
			return "Google";
		}else if (ug == 1) {
			return "Bing";
		}else if (ug == 2) {
			return "YouTube";
		}else {
			return "DuckDuckGo";
		}
	}
	
	public static void generateIdentity() {
		FontManager.setFont(FontManager.getFonts("MADE Evolve Sans",
				"Biko", "Highway Gothic", "Young"), 16);
		
		final eActiveConsoledBar stat = eActiveConsoledBar.main();
		stat.setResizable(true);
		stat.setTitle("Identity Generator 1.0");
		stat.setText("Generating Name...");
		stat.setIndeterminate(true);
		
		final DataSet<String, String> name = generateName();
		
		stat.appendConsole("Your name is: " + name.getA() + " "  + name.getB());
		
		final String id = username(name);
		
		stat.appendConsole("Your username is: " + id);
		
		stat.setText("Creating identity...");
		stat.setIndeterminate(false);
		
		final int spoofs = MathUtil.randInt(8, 162);
		
		stat.appendConsole("Changing identity...");
		
		for (int i = 0; i < spoofs; i++) {
			try {
				final int ug = MathUtil.randInt(0, 3);
				String url;
				if (ug == 0) {
					url = createGoogleSearch(processVars(ArrayUtils.random(SEARCH_TERMS)));
				}else if (ug == 1) {
					url = createBingSearch(processVars(ArrayUtils.random(SEARCH_TERMS)));
				}else if (ug == 2) {
					url = createYoutubeSearch(processVars(ArrayUtils.random(SEARCH_TERMS)));
				}else {
					url = createDuckDuckGoSearch(processVars(ArrayUtils.random(SEARCH_TERMS)));
				}
				
				final URL ur = URIUtil.safeCreate(url);
				final HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
				conn.setRequestProperty("User-Agent", URIUtil2.RandomUserAgent.getRandomUserAgent());
				conn.connect();
				try {
					Thread.sleep(MathUtil.randInt(200, 2700));
				}catch (Exception e) {}
				conn.disconnect();
				
				final double prg = (double) ((double) i / spoofs) * 100;
				
				stat.setStatus((int) prg);
				stat.appendConsole("Changing identity... [" + i + "/" + (spoofs - 1) + "] [" + gt_int(ug) + "] \n - " + url);
			}catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				Thread.sleep(MathUtil.randInt(500, 2500));
			} catch (InterruptedException e) {
			}
		}
		
		stat.setStatus(100);
		stat.setText("Finished.");
	}
	
	public static void main(final String[] args) {
		generateIdentity();
	}
}
